﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using WPFDemoMVVM.Helpers;
using WPFDemoMVVM.Model;
using WPFLocalizeExtension.Engine;

namespace WPFDemoMVVM.ViewModel
{
    public partial class DataGridAndPageViewModel: ObservableObject
    {
        public List<EmployeeModel> employees;

        [ObservableProperty]
        ICollectionView collectionView;

        [ObservableProperty]
        string queryKeyword;

        [ObservableProperty]
        IList selectedItems;

        [ObservableProperty]
        private double totalSalary;

        [ObservableProperty]
        public Paging2ViewModel pagination;

        public ObservableCollection<LanguageItem> Languages { get; } = new ObservableCollection<LanguageItem>
        {
            new LanguageItem { Tag = "zh-CN", Name = "中文" },
            new LanguageItem { Tag = "en", Name = "英文" }
        };

        [ObservableProperty]
        private string selectedLanguage;

        public ICommand CloseCommand { get; }
        public DataGridAndPageViewModel()
        {           
            Pagination = new Paging2ViewModel();
            Pagination.PageChanged += OnPageChanged;
            ChangeLanguage("zh-CN");

            IEnumerable<EmployeeModel> employees = EmployeeModel.FakeMany(10);
            this.employees = new List<EmployeeModel>(employees);

            var pageData = this.employees
                .Skip((Pagination.CurrentPage - 1) * Pagination.ItemsPerPage)
                .Take(Pagination.ItemsPerPage)
                .Select((e, i) =>
                {
                    e.Index = (Pagination.CurrentPage - 1) * Pagination.ItemsPerPage + i + 1;
                    return e;
                }).ToList();

            CollectionView = CollectionViewSource.GetDefaultView(pageData);

            Pagination.TotalItems = this.employees.Count;


            CloseCommand = new RelayCommand(() =>
            {
                // 发送关闭窗口的消息
                WeakReferenceMessenger.Default.Send(new CloseWindowMessage("DataGridAndPageView"));
            });
        }


        partial void OnQueryKeywordChanged(string value)
        {
            Pagination.CurrentPage = 1; // 筛选后回到第一页
            OnPageChanged(Pagination.CurrentPage, Pagination.ItemsPerPage);
        }

        [RelayCommand]
        private void ChangeLanguage(string languageCode)
        {
            SelectedLanguage = languageCode;
            Pagination.Language = languageCode;
            var culture = new CultureInfo(languageCode);
            Thread.CurrentThread.CurrentCulture = culture;
            Thread.CurrentThread.CurrentUICulture = culture;
            LocalizeDictionary.Instance.Culture = culture;
        }


        private void OnPageChanged(int currentPage, int pageSize)
        {
            var filtered = employees.Where(e => string.IsNullOrEmpty(QueryKeyword) || e.Name.Contains(QueryKeyword, StringComparison.OrdinalIgnoreCase)).ToList();
            Pagination.TotalItems = filtered.Count;

            //重新生成索引 number从1开始
            var pageData = filtered
                .Skip((currentPage - 1) * pageSize)
                .Take(pageSize)
                .Select((e, i) =>
                {
                    e.Index = (currentPage - 1) * pageSize + i + 1;
                    return e;
                }).ToList();

            CollectionView = CollectionViewSource.GetDefaultView(pageData);
            
        }


        [RelayCommand]
        public void Add()
        {
            employees.Add(EmployeeModel.FakeOne());
            OnPageChanged(Pagination.CurrentPage, Pagination.ItemsPerPage);
        }

        [RelayCommand]
        public void Delete()
        {
            if (SelectedItems != null)
            {
                employees.RemoveAll(e => SelectedItems.Contains(e));
                OnPageChanged(Pagination.CurrentPage, Pagination.ItemsPerPage);
            }
        }


        [RelayCommand]
        public void CalculateSumSalary()
        {
            if (SelectedItems != null)
            {
                var sum = SelectedItems.Cast<EmployeeModel>().Sum(x => ((EmployeeModel)x).Salary);
                TotalSalary = sum;
            }

            foreach (var item in employees)
            {
                item.IsSelected = item.Salary > 15000;
            }
            collectionView.Refresh();
        }



    }
}
